package com.web.servlet;

import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import java.util.Vector;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.web.constant.Constant;
import com.web.po.Huodong;
import com.web.po.Penson;
import com.web.utils.Utils;
import com.web.vo.ResultVO;

@WebServlet("/manager/*")
public class ManagerServlet extends BaseServlet{
	private static final long serialVersionUID = 1L;
	
	public ServletContext app = null;

	@Override
	public boolean before(HttpServletRequest request, HttpServletResponse response, Map<String, Object> data) {
		app = request.getServletContext();
		if (Utils.isWindow()) {
			return true;
		}
		HttpSession session = request.getSession();
		if (session.getAttribute(Constant.ManagerSessionName)==null) {
			ResultVO res = new ResultVO();
			res.setMessage("管理员未登录");
			res.setStatus(Constant.ISNOT_LOGIN);
			returnJSON(response, res);
			return false;
		} else {
			return true;
		}
	}

	public ResultVO addHuodong(HttpServletRequest request, HttpServletResponse response, Map<String, Object> data) {
		ResultVO res = new ResultVO();
		if (null==app.getAttribute(Constant.AppHuodongName)) {
			app.setAttribute(Constant.AppHuodongName, new Vector<Huodong>());
		}
		Vector<Huodong> huodongs = (Vector<Huodong>)app.getAttribute(Constant.AppHuodongName);
		Huodong huodong = new Huodong();
		huodong.setH_id(UUID.randomUUID().toString());
		huodong.setIsbegin(false);
		huodong.setPensons(new Vector<Penson>());
		String name = (String)data.get("name");
		huodong.setName(name);
		huodongs.add(huodong);
		synchronized (app) {
			app.setAttribute(Constant.AppHuodongName, huodongs);
		}
		res.setData(huodong);
		res.setStatus(Constant.SUCCESS_STATUS);
		return res;
	}
	
	public ResultVO deHuodong(HttpServletRequest request, HttpServletResponse response, Map<String, Object> data) {
		ResultVO res = new ResultVO();
		String h_id = (String)data.get("h_id");
		Vector<Huodong> huodongs = (Vector<Huodong>)app.getAttribute(Constant.AppHuodongName);
		int num = 0;
		Iterator<Huodong> item = huodongs.iterator();
		while (item.hasNext()) {
			Huodong huodong = item.next();
			if (huodong.getH_id().equals(h_id)) {
				item.remove();
				num++;
			}
		}
		synchronized (app) {
			app.setAttribute(Constant.AppHuodongName, huodongs);
		}
		res.setData(num);
		res.setStatus(Constant.SUCCESS_STATUS);
		return res;
	}
	
	public ResultVO getHuodong(HttpServletRequest request, HttpServletResponse response, Map<String, Object> data) {
		ResultVO res = new ResultVO();
		String h_id = (String)data.get("h_id");
		Vector<Huodong> huodongs = (Vector<Huodong>)app.getAttribute(Constant.AppHuodongName);
		Huodong reshuodong = null;
		Iterator<Huodong> item = huodongs.iterator();
		while (item.hasNext()) {
			Huodong huodong = item.next();
			if (huodong.getH_id().equals(h_id)) {
				reshuodong = huodong;
				break;
			}
		}
		
		res.setData(reshuodong);
		res.setStatus(Constant.SUCCESS_STATUS);
		
		return res;
	}
	
	public ResultVO getAllHuodong(HttpServletRequest request, HttpServletResponse response, Map<String, Object> data) {
		ResultVO res = new ResultVO();
		Vector<Huodong> huodongs = (Vector<Huodong>)app.getAttribute(Constant.AppHuodongName);
		res.setData(huodongs);
		res.setStatus(Constant.SUCCESS_STATUS);
		return res;
	}
	
	public ResultVO deAllHuodong(HttpServletRequest request, HttpServletResponse response, Map<String, Object> data) {
		ResultVO res = new ResultVO();
		app.removeAttribute(Constant.AppHuodongName);
		res.setStatus(Constant.SUCCESS_STATUS);
		return res;
	}
	
	public ResultVO isbeginHuodong(HttpServletRequest request, HttpServletResponse response, Map<String, Object> data) {
		ResultVO res = new ResultVO();
		String h_id = (String)data.get("h_id");
		Vector<Huodong> huodongs = (Vector<Huodong>)app.getAttribute(Constant.AppHuodongName);
		Huodong reshuodong = null;
		Iterator<Huodong> item = huodongs.iterator();
		while (item.hasNext()) {
			Huodong huodong = item.next();
			if (huodong.getH_id().equals(h_id)) {
				reshuodong = huodong;
				item.remove();
				break;
			}
		}
		reshuodong.setIsbegin((Boolean)data.get("isbegin"));
		huodongs.add(reshuodong);
		synchronized (app) {
			app.setAttribute(Constant.AppHuodongName, huodongs);
		}
		res.setData(reshuodong);
		res.setStatus(Constant.SUCCESS_STATUS);
		return res;
	}
	
}
